package com.cy.ppj.web.admin.product;

import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.ao.product.ProductAddAO;
import com.cy.ppj.model.ao.product.ProductEditAO;
import com.cy.ppj.model.domian.city.City;
import com.cy.ppj.model.domian.product.Product;
import com.cy.ppj.model.domian.product.ProductRate;
import com.cy.ppj.model.enums.ProductStatus;
import com.cy.ppj.model.query.ProductAdminQuery;
import com.cy.ppj.model.vo.ProductAdminVO;
import com.cy.ppj.service.city.CityService;
import com.cy.ppj.service.product.ProductService;
import com.cy.ppj.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @description: 产品
 * @author: Ocean
 * @create: 2018-07-17 14:35
 **/
@Slf4j
@RestController
@RequestMapping("/admin/product")
@Api(tags = "后台-产品管理")
public class AdminProductController extends BaseController {

    @Autowired
    private ProductService productService;
    @Autowired
    private CityService cityService;

    @ApiOperation("新增产品")
    @PostMapping("/add")
    public Result add(@Validated @RequestBody ProductAddAO productAddAO, BindingResult bindingResult) {
        this.checkError(bindingResult);
        productService.addProduct(productAddAO);
        return ok();
    }

    @ApiOperation("编辑产品")
    @PostMapping("/edit")
    public Result edit(@Validated @RequestBody ProductEditAO productEditAO, BindingResult bindingResult) {
        this.checkError(bindingResult);
        productService.updateProduct(productEditAO);
        return ok();
    }

    @ApiOperation("产品列表")
    @GetMapping("/list")
    public PageBean<ProductAdminVO> list(ProductAdminQuery productAdminQuery) {
        return productService.adminProducts(productAdminQuery);
    }
    
    @ApiOperation("产品详情")
    @GetMapping("/detail")
    public Result detail(@ApiParam(value = "产品id", example = "0", required = true) @RequestParam Long productId) {
    	return productService.queryProductDetailAdmin(productId);
    } 
    
    @ApiOperation("上下架产品")
    @GetMapping("/update/status")
    public Result updateProductStatus(@ApiParam(value = "产品id", example = "0", required = true) @RequestParam Long productId,
    		@ApiParam(value = "产品状态", required = true) @RequestParam ProductStatus productStatus) {
    	return productService.updateProductStatus(productId, productStatus);
    }
    
    @ApiOperation("获取城市列表")
    @GetMapping("/city/list")
    public Result cityList() {
    	List<City> list = cityService.getAllCityInCache();
    	return ok("", list);
    }
    
    @ApiOperation("获取全部上架的产品列表")
    @GetMapping("/all/product/list")
    public Result<List<Product>> selectAllProductsIncache() {
    	List<Product> list = productService.selectAllProductsIncache();
    	return ok("", list);
    } 
    
    @ApiOperation("查询产品期限、利率、还款方式")
    @GetMapping("/get/rate/list")
    public Result<List<ProductRate>> selectProductRateListByProductId(@RequestParam(value = "productId") Long productId) {
    	List<ProductRate> list = productService.selectProductRateListByProductId(productId);
    	return ok("", list);
    }
    
    @ApiOperation("后台录单可选的产品列表")
    @GetMapping("/get/list/for/add/order")
    public Result<List<Product>> selectProductsForAddOrder() {
    	List<Product> list = productService.selectProductsForAddOrderInCache();
    	return ok("", list);
    }
    
}
